package com.thb.user.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thb.exception.ExceptionCodeUtil;
import com.thb.exception.ServiceException;
import com.thb.logs.utils.LogUtils;
import com.thb.user.model.UserCode;
import com.thb.user.model.UserInfo;
import com.thb.user.service.IUserCodeService;
import com.thb.user.service.IUserInfoService;
import com.thb.utils.ResponseTool;
import com.thb.utils.SidUtils;

@Controller
public class UserController {
	
	private static final Logger logger = LogUtils.getUserLog();
	
	private static final byte UTYPER = 1;
	
	@Autowired
	private IUserCodeService usercodeService;
	
	@Autowired
	private IUserInfoService userInfoService;
	
	public UserController(){
		
	}
	
	@ResponseBody
	@RequestMapping(value="/user/register",method=RequestMethod.POST)
	public Object register(HttpServletRequest request){
		
		String json = request.getParameter("args");
		logger.info(String.format("args is : %s",json));
		JSONObject obj = JSON.parseObject(json);
		String usercode = obj.getString("usercode");
		String passwd = obj.getString("passwd");
		
		String random = obj.getString("random");
		
		if(usercode == null || "".equals(usercode)){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "账号不能为空");
		}
		
		if(passwd==null || passwd.equals("")){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "密码不能为空");
		}
		
		
		if(usercode.length() > 40){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "账号长度不能超过40");
		}
		usercodeService.checkRandom(usercode, random);
		usercodeService.add(passwd, UTYPER, usercode);
		
		return ResponseTool.returns();
		
	}
	
	@ResponseBody
	@RequestMapping(value="/user/login",method=RequestMethod.POST)
	public Object login(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is : %s",json));
		JSONObject obj = JSON.parseObject(json);
		String usercode = obj.getString("usercode");
		String passwd = obj.getString("passwd");
		
		if(usercode == null || "".equals(usercode)){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "用户名不能为空");
		}
		
		if(passwd == null || "".equals(passwd)){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "密码不能为空");
		}
		
		String sid = usercodeService.login(usercode,passwd,UTYPER);
		JSONObject js = new JSONObject();
		js.put("sid", sid);
		return ResponseTool.returns(js);
		
	}
	
	@ResponseBody
	@RequestMapping(value="/user/updateUserInfo",method=RequestMethod.POST)
	public Object updateUserInfo(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is : %s",json));
		String sid = request.getParameter("sid");
		int uid = SidUtils.getUid(sid);
		JSONObject obj = JSONObject.parseObject(json);
		JSONObject uobj =  (JSONObject)obj.get("userInfo");
		UserInfo userInfo = JSONObject.parseObject(uobj.toJSONString(), UserInfo.class);
		
		userInfoService.update(userInfo,uid);
		
		return ResponseTool.returns();
		
	}
	
	
	
	@ResponseBody
	@RequestMapping(value="/user/getRandom",method=RequestMethod.POST)
	public Object getRandom(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is : %s",json));
		JSONObject js = JSONObject.parseObject(json);
		String usercode = js.getString("usercode");
		byte type = js.getByteValue("type");
		String ss = usercodeService.getRandom(usercode);
		
		return ResponseTool.returns(ss);
		
	}
	
	@ResponseBody
	@RequestMapping(value="/user/getUserInfo",method=RequestMethod.POST)
	public Object getUserInfo(HttpServletRequest request){
		String sid = request.getParameter("sid");
		logger.info(String.format("args is : %s",sid));
		
		UserCode user = usercodeService.getBySid(sid);
		UserInfo info = userInfoService.findInfoByUid(user.getUid());
		JSONObject json = new JSONObject();
		json.put("user", info);
		
		return ResponseTool.returns(json);
		
	}
	
	@ResponseBody
	@RequestMapping(value="/user/updatePasswd",method=RequestMethod.POST)
	public Object updatePasswd(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is : %s",json));
		String sid = request.getParameter("sid");
		JSONObject obj = JSONObject.parseObject(json);
		String passwd =  (String)obj.get("passwd");
		String newpasswd =  (String)obj.get("newpasswd");
		
		if(null == passwd || "".equals(passwd)){
			throw new ServiceException(ExceptionCodeUtil.ValidErrorCode, "原密码不能为空");
		}
		
		if(null == newpasswd || "".equals(newpasswd)){
			throw new ServiceException(ExceptionCodeUtil.ValidErrorCode, "新密码不能为空");
		}
		
		
		UserCode user = usercodeService.getBySid(sid);
		if(!user.getPasswd().equals(passwd)){
			throw new ServiceException(ExceptionCodeUtil.ValidErrorCode, "原密码不正确");
		}
		usercodeService.updatePasswd(user.getUid(), newpasswd);
		return ResponseTool.returns();
		
	}
	
	@ResponseBody
	@RequestMapping(value="/user/updateForgetPasswd",method=RequestMethod.POST)
	public Object updateForgetPasswd(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is : %s",json));
		JSONObject obj = JSONObject.parseObject(json);
		String random =  (String)obj.get("random");
		String usercode =  (String)obj.get("usercode");
		String newpasswd =  (String)obj.get("newpasswd");
		
		if(null == random || "".equals(random)){
			throw new ServiceException(ExceptionCodeUtil.ValidErrorCode, "验证码不能为空");
		}
		if(null == usercode || "".equals(usercode)){
			throw new ServiceException(ExceptionCodeUtil.ValidErrorCode, "邮箱不能为空");
		}
		
		if(null == newpasswd || "".equals(newpasswd)){
			throw new ServiceException(ExceptionCodeUtil.ValidErrorCode, "新密码不能为空");
		}
		
		
		usercodeService.checkRandom(usercode, random);
		UserCode user = usercodeService.findInfoByUsercode(usercode);
		usercodeService.updatePasswd(user.getUid(), newpasswd);
		
		return ResponseTool.returns("");
		
	}
	
/*	@ResponseBody
	@RequestMapping(value="/user/forgetPasswd",method=RequestMethod.POST)
	public Object forgetPasswd(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is : %s",json));
		JSONObject obj = JSONObject.parseObject(json);
		String email =  (String)obj.get("email");
		
		if(null == email || "".equals(email)){
			throw new ServiceException(ExceptionCodeUtil.ValidErrorCode, "邮箱不能为空");
		}
		
		userService.forgetPasswd(email);
		
		return ResponseTool.returns("");
		
	}
	*/
}
